package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     *
     * @param dto
     */
    public void addShoppingCart(ShoppingCartDTO dto) {
        //1.根据条件查询当前商品是否在购物车中
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart= shoppingCartMapper.queryShoppingCar(shoppingCart);
        //2.如果已经存在，则更新购买的数量
        if(shoppingCart!=null){
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else{
            shoppingCart=new ShoppingCart();
            //3.1 判断当前添加的是菜品还是套餐
            Long dishId = dto.getDishId();
            if(dishId!=null){//如果是菜品
                //根据菜品id查询菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setDishFlavor(dto.getDishFlavor());
                shoppingCart.setDishId(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else{//如果是套餐
                //根据套餐id查询套餐
                Setmeal setmeal = setmealMapper.getByid(dto.getSetmealId());
                shoppingCart.setSetmealId(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //3.如果不存在，则插入数据，数量是1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public Result list() {
        //获取用户userid
        Long userId = BaseContext.getCurrentId();
        //调用dao查看购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(userId);
        //返回数据
        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public Result clean() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //调用dao查看购物车
        shoppingCartMapper.clean(userId);
        //返回数据
        return Result.success();
    }

    /**
     * 删除购物车中一个商品
     *
     * @param dto
     * @return
     */
    @Override
    public Result subShoppingCart(ShoppingCartDTO dto) {

        //处理参数
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);

        //获取用户userid
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //设置查询条件,查询当前登录用户的购物车数据
        ShoppingCart shoppingCartOne = shoppingCartMapper.queryShoppingCar(shoppingCart);

        //判断商品是否只有一个
        Integer number = shoppingCartOne.getNumber();
        if (number == 1){
            //数量只有一个就根据条件删除商品
            shoppingCartMapper.deleteById(shoppingCartOne.getId());
        }
        //如果有多个则数量-1
        shoppingCartOne.setNumber(shoppingCartOne.getNumber()-1);
        shoppingCartMapper.updateNumberById(shoppingCartOne);

        //返回数据
        return Result.success();
    }

}
